/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Mar 16, 2012  12:17:46 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.common
 * File Name : PermanentReceiptServices.java
 * TODO
 */
package jpa.service.promis.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.common.MktPaymentReceiptDetail;
import jpa.entitys.promis.common.MktReceiptDetail;
import jpa.entitys.promis.common.PermanentReceipt;
import jpa.entitys.promis.common.PermanentReceiptPK;
import jpa.entitys.promis.common.PmsPaymentReceivedDetail;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class PermanentReceiptServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(PermanentReceiptServices.class);
	
	public PermanentReceiptServices(EntityManager em) {
		super(em);
	}

	public List<PermanentReceipt> findAll() throws Exception 
	{
		return super.ConvertToYoureObject(super.findAll("PermanentReceipt.findAll"));
	}
	
	public PermanentReceipt findById(PermanentReceiptPK id) throws Exception {		
		
		return super.ConvertToYoureObject(super.find(id, "PermanentReceipt.findById"));				
	}
	
	public PermanentReceipt findBySapReceiptedNoWithoutSyag(String syncExternalRefNo, String status) throws Exception {
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		
		mapWhereValue.put("syncExternalRefNo", syncExternalRefNo);
		mapWhereValue.put("status", status);
		
		List<PermanentReceipt> lst = super.ConvertToYoureObject(super.find("PermanentReceipt.findBySapReceiptedNoWithoutSyag", mapWhereValue));
		if(CommonService.getSize(lst) == 1)
			return lst.get(0);
		else if (CommonService.getSize(lst) > 1)
			throw new Exception("Error SAP Receipt No = "+ CommonService.IsNullRString(syncExternalRefNo) +" has more then one ["+ lst.size() +"] record(s).");
		else 
			return null;
	}
	
	public PermanentReceipt findBySapReceiptedNo(String syncExternalRefNo) throws Exception {		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("syncExternalRefNo", syncExternalRefNo);
		mapWhereValue.put("status", "A");
		
		List<PermanentReceipt> lst = super.ConvertToYoureObject(super.find("PermanentReceipt.findBySapReceiptedNo", mapWhereValue));
		if(CommonService.getSize(lst) == 1)
			return lst.get(0);
		else if (CommonService.getSize(lst) > 1)			
			throw new Exception("Error SAP Receipt No = "+ CommonService.IsNullRString(syncExternalRefNo) +" has more then one ["+ lst.size() +"] record(s).");
		else 
			return null;
	}
	
	public PermanentReceipt InsertedOrUpdated(PermanentReceipt vlaue) throws Exception {
		PermanentReceipt obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{					
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.inserted(obj);
			}else{				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
		}catch(Exception e)
		{
			System.err.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return obj;
	}
	
	public PermanentReceipt Updated(PermanentReceipt vlaue) throws Exception {
		PermanentReceipt obj = vlaue;
		try{
			
			if(vlaue.getId() != null)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}				
		}catch(Exception e)
		{
			System.err.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return obj;
	}
	
	public List<PermanentReceipt> Inserted(List<PermanentReceipt> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(PermanentReceipt obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
				
		}catch(Exception e)
		{
			System.err.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		
		return vlauelist;
	}
	
	public List<PermanentReceipt> Updated(List<PermanentReceipt> vlauelist) throws Exception {		
		
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			for(PermanentReceipt obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{					
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			System.err.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}
	
	public List<PermanentReceipt> Inserted(	List<PermanentReceipt> headerlist, 
											List<PmsPaymentReceivedDetail> receivedlist, 
											List<MktReceiptDetail> receiptedList, 
											List<MktPaymentReceiptDetail> payReceiptedList) throws Exception {
		try{
			List<CommonEntity> headcommon_lst = new ArrayList<CommonEntity>();
			List<CommonEntity> received_lst = new ArrayList<CommonEntity>();
			List<CommonEntity> receipted_lst = new ArrayList<CommonEntity>();
			List<CommonEntity> payReceipted_lst = new ArrayList<CommonEntity>();
			
			for(PermanentReceipt obj : headerlist)
			{
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				headcommon_lst.add(obj);
			}
			
			for(PmsPaymentReceivedDetail obj : receivedlist)
			{
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				received_lst.add(obj);
			}
			
			for(MktReceiptDetail obj : receiptedList)
			{
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				receipted_lst.add(obj);
			}
			
			for(MktPaymentReceiptDetail obj : payReceiptedList)
			{
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				payReceipted_lst.add(obj);
			}
			
			if(CommonService.getSize(headcommon_lst) > 0 && 
															CommonService.getSize(received_lst) > 0 && 
															CommonService.getSize(receipted_lst) > 0 && 
															CommonService.getSize(payReceipted_lst) > 0)
			{
				EntityTransaction t = null;
				try{			
			        t = getEm().getTransaction();
			        t.begin();	        
			        
			        getEm().persist(headcommon_lst);
			        getEm().persist(received_lst);
			        getEm().persist(receipted_lst);
			        getEm().persist(payReceipted_lst);
			        
				}catch(Exception e)
				{			
					log.error(e);
					t.rollback();
					throw e;
					
				}finally{
					t.commit();
				}
				return headerlist;
			}else
				throw new Exception("Error Can't Updated");
			
		}catch(Exception e)
		{
			System.err.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
	}
	
	public List<PermanentReceipt> Updated(	List<PermanentReceipt> headerlist, 
											List<PmsPaymentReceivedDetail> receivedlist, 
											List<MktReceiptDetail> receiptedList, 
											List<MktPaymentReceiptDetail> payReceiptedList) throws Exception {
		try{
			List<CommonEntity> headcommon_lst = new ArrayList<CommonEntity>();
			List<CommonEntity> received_lst = new ArrayList<CommonEntity>();
			List<CommonEntity> receipted_lst = new ArrayList<CommonEntity>();
			List<CommonEntity> payReceipted_lst = new ArrayList<CommonEntity>();
			
			for(PermanentReceipt obj : headerlist)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				headcommon_lst.add(obj);
			}
			
			for(PmsPaymentReceivedDetail obj : receivedlist)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				received_lst.add(obj);
			}
			
			for(MktReceiptDetail obj : receiptedList)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				receipted_lst.add(obj);
			}
			
			for(MktPaymentReceiptDetail obj : payReceiptedList)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				payReceipted_lst.add(obj);
			}
			
			if(CommonService.getSize(headcommon_lst) > 0 && 
															 CommonService.getSize(received_lst) > 0 && 
															 CommonService.getSize(receipted_lst) > 0 && 
															 CommonService.getSize(payReceipted_lst) > 0)
			{
				EntityTransaction t = null;
				try{			
			        t = getEm().getTransaction();
			        t.begin();	        
			        
			        getEm().merge(headcommon_lst);
			        getEm().merge(received_lst);
			        getEm().merge(receipted_lst);
			        getEm().merge(payReceipted_lst);
			        
				}catch(Exception e)
				{			
					System.err.println(e.getMessage());
					log.error(e.getMessage());
					t.rollback();
					throw e;
					
				}finally{
					t.commit();
				}
				return headerlist;
			}else
				throw new Exception("Error Can't Updated");
		}catch(Exception e)
		{
			System.err.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
	}
}
